package com.docMan.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.docMan.dao.BaseDao;
import com.docMan.dao.TchumanDao;
import com.docMan.dao.pojo.DocDirectory;
import com.docMan.dao.pojo.Tchuman;
import com.docMan.dao.pojo.Tcrole;
import com.docMan.service.DocAdminService;
import com.docMan.service.DocDirectoryService;
import com.docMan.service.TchumanService;
import com.docMan.service.TcroleService;
import com.docMan.utils.FinalConfig;
import com.docMan.utils.MD5Utils;
import com.docMan.utils.ProjectUtils;
@Path("/tchumanService")
@Produces(MediaType.APPLICATION_JSON)
@Service("tchumanService")
public class TchumanServiceImpl extends BaseServiceImpl<Tchuman> implements TchumanService {
	private TchumanDao tchumanDao;
	private HttpSession session;
	private TcroleService tcroleService;
	private DocAdminService docAdminService;
	private DocDirectoryService docDirectoryService;
	
	@Autowired
	public void setDocDirectoryService(DocDirectoryService docDirectoryService) {
		this.docDirectoryService = docDirectoryService;
	}

	@Autowired
	public void setDocAdminService(DocAdminService docAdminService) {
		this.docAdminService = docAdminService;
	}

	@Resource(name="tchumanDao")
	@Override
	public void setDao(BaseDao<Tchuman> dao) {
		// TODO Auto-generated method stub
		super.setDao(dao);
	}

	@Resource(name="tcroleService")
	public void setTcroleService(TcroleService tcroleService) {
		this.tcroleService = tcroleService;
	}

	@Autowired
	public void setSession(HttpSession session) {
		this.session = session;
	}
//	@Resource(name="tchumanDao")
	@Autowired
	public void setTchumanDao(TchumanDao tchumanDao) {
		this.tchumanDao = tchumanDao;
	}
	
	@Override
	@GET
	@Path("/getTchumanByunitid/{unitId}")
	public String getHuamnByUnitId(@PathParam("unitId")int unitId) {
		String hql="select h from Tchuman h where h.tcunitId in (select u.unitId from Tcunit u where u.seniorUnitId=? )";
		String key=hql.replaceAll(" ", "");
		List<Tchuman> humanList=tchumanDao.find(key,hql, unitId);
		String hql2="select h from Tchuman h where h.tcunitId=?";
		humanList.addAll(tchumanDao.find(ProjectUtils.getMemcachedKey(hql2),hql2,unitId));
		return ProjectUtils.gson.toJson(humanList);
	}
	
	
	@Override
	@POST
	@Path("/getHumanById")
	public String getHumanById(@FormParam("humanId")int humanId) {
		String hql="select h from Tchuman h where h.humanId =?";
		humanId=(int) session.getAttribute("humanId");
		List<Tchuman> list=tchumanDao.find(ProjectUtils.getMemcachedKey(hql, humanId), hql, humanId);
		
		return ProjectUtils.gson.toJson(list);
	}
	@POST
	@Path("/getHumanByUid")
	@Override
	public String getHumanByUnitId(@FormParam("unitId")int unitId) {
		String hql="select h from Tchuman h where h.unitid=?";
		List<Tchuman> list=tchumanDao.find(ProjectUtils.getMemcachedKey(hql, unitId),hql,unitId);
		return ProjectUtils.gson.toJson(list);
	}

	@Override
	public int login(String username, String password) {
		String hqlString="select t from Tchuman t where t.humanname=? or t.humancode=? and t.validflag =?";
		List<Tchuman> humansList=find(hqlString, username,username,1);
		if(humansList.size()<1){
			return FinalConfig.LOGIN_NO_USER;
		}else{
			Tchuman human=humansList.get(0);
			if(human.getHumanpassword()==null){
				tcroleService.getRole(human.getHumanid());
				docAdminService.isAdmin();
//				tcroleService.isAdmin();
				DocDirectory personalDisk=docDirectoryService.createPersonalDirectory();
				session.setAttribute(FinalConfig.PersonalDirId, personalDisk.getId());
				return FinalConfig.LOGIN_SUCCESS;
			}
			String md5Pwd = MD5Utils.getMD5((human.getHumanid()+password).getBytes()).toLowerCase();
			System.out.println(md5Pwd);
			System.out.println(human.getHumanpassword());
			if(md5Pwd.equals(human.getHumanpassword().toLowerCase())){
				tcroleService.getRole(human.getHumanid());
				docAdminService.isAdmin();
//				tcroleService.isAdmin();
				DocDirectory personalDisk=docDirectoryService.createPersonalDirectory();
				session.setAttribute(FinalConfig.PersonalDirId, personalDisk.getId());
				return FinalConfig.LOGIN_SUCCESS;
			}else{
				return FinalConfig.LOGIN_PASSWORD_WRONG;
			}
		}
	}

	@Override
	@Path("/getHumanByName")
	@POST
	public String getHumanByName(@FormParam("username")String username) {
		List<Tchuman> list=find("select t from Tchuman t where t.humanname =?",username);
		if(list.size()>0){
			put("human", list.get(0));
			putSuccess();
		}else{
			putMessage("没有搜索到这个人");
			putFalse();
		}
		return getMap();
	}
}
